{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division\n",
    "from tqdm import *\n",
    "import numpy as np \n",
    "import scipy.stats as st"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def round(index):\n",
    "    \n",
    "    i = 0\n",
    "      \n",
    "    odd_previous_heads = False\n",
    "    \n",
    "    while(True):\n",
    "        # 0 = heads, 1=tails\n",
    "        elem = np.random.choice([0,1])\n",
    "        i += 1\n",
    "        \n",
    "        if(elem == 0):\n",
    "            if(odd_previous_heads == True):\n",
    "                odd_previous_heads = False\n",
    "            else:\n",
    "                odd_previous_heads = True\n",
    "                \n",
    "        else:\n",
    "            if(odd_previous_heads == True):\n",
    "                return i\n",
    "            else:\n",
    "                continue        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 999999/999999 [00:28<00:00, 34761.91it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.00127700128\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "num_rounds = []\n",
    "\n",
    "for i in trange(1,1000000):\n",
    "    num_rounds.append(round(i))\n",
    "    \n",
    "print(np.mean(np.array(num_rounds)))    \n",
    "    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Sandbox Venv (Python 2)",
   "language": "python",
   "name": "sandbox-venv-2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
